package GUI;


import java.awt.BorderLayout;
//import java.awt.Color;
import java.awt.EventQueue;
//import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
//import java.util.ArrayList;
//import java.util.Set;


import javax.swing.JButton;
//import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
//import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


//import mapa.Coordenada;
//import mapa.Domicilio;
import odpgl.Implementacion;

import org.openstreetmap.gui.jmapviewer.Coordinate;
import org.openstreetmap.gui.jmapviewer.JMapViewer;
import org.openstreetmap.gui.jmapviewer.MapMarkerDot;
//import org.openstreetmap.gui.jmapviewer.MapPolygonImpl;
import org.openstreetmap.gui.jmapviewer.interfaces.MapMarker;
//import org.openstreetmap.gui.jmapviewer.interfaces.MapPolygon;


import pedidos.Pedido;
import clientes.Cliente;
//import clientes.Historial;


//import java.awt.Frame;
import java.awt.Point;
import java.awt.Dimension;
import java.util.Locale;
import java.awt.Rectangle;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;



public class ventanaPedPendientesMapa extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5003567696579373720L;
	private JFrame dialogo;
	private JMapViewer mapa;
	public Implementacion pedidosGeoLoc = new Implementacion();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ventanaMapeoRecorrido window = new ventanaMapeoRecorrido();
					window.dialogo.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ventanaPedPendientesMapa() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		dialogo = new JFrame();
		dialogo.setBounds(new Rectangle(0, 0, 0, 0));
		dialogo.setAlwaysOnTop(true);
		dialogo.setResizable(false);
		dialogo.setPreferredSize(new Dimension(100, 200));
		dialogo.setLocale(new Locale("es", "AR"));
		dialogo.setLocationByPlatform(true);
		dialogo.setName("muestramapa");
		dialogo.setVisible(true);
		dialogo.setBounds(150, 0, 1024, 700);
		dialogo.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		JPanel panelSuperior = new JPanel();
		dialogo.getContentPane().add(panelSuperior, BorderLayout.NORTH);

		JPanel panelInferior = new JPanel();
		dialogo.getContentPane().add(panelInferior, BorderLayout.SOUTH);

		final JSlider slider = new JSlider();
		slider.setValue(10);
		slider.setMaximum(19);
		slider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent arg0) {
				int valor = slider.getValue();
				mapa.setZoom(valor);
			}
		});
		panelInferior.add(slider);

		JButton btnVolver = new JButton("Volver");
		btnVolver.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dialogo.dispose();
			}
		});
		panelInferior.add(btnVolver);

		JPanel panelIzquierdo = new JPanel();
		dialogo.getContentPane().add(panelIzquierdo, BorderLayout.WEST);

		JPanel panelDerecho = new JPanel();
		dialogo.getContentPane().add(panelDerecho, BorderLayout.EAST);

		mapa = new JMapViewer();
		mapa.setOpaque(false);
		mapa.setPreferredSize(new Dimension(800, 600));
		mapa.setMapMarkerVisible(true);
		mapa.setLocation(new Point(0, 0));
		mapa.setMinimumSize(new Dimension(0, 0));
		mapa.setMaximumSize(new Dimension(2147483647, 2147483647));
		mapa.addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseDragged(MouseEvent arg0) {
				Coordinate c = mapa.getPosition();
				dialogo.setTitle(c.getLat() + ", " + c.getLon());
			}
		});
		mapa.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				slider.setValue(mapa.getZoom());
			}
		});
		mapa.addMouseWheelListener(new MouseWheelListener() {
			public void mouseWheelMoved(MouseWheelEvent arg0) {
				slider.setValue(mapa.getZoom());
			}
		});
		mapa.setZoomContolsVisible(false);
		mapa.setDisplayPositionByLatLon(-34.522044,-58.900895, 10);

		dialogo.getContentPane().add(mapa, BorderLayout.CENTER);

		mostrarClientes();

	}

	// Muestra los clientes de la empresa
	private void mostrarClientes() {
		for (Pedido c : pedidosGeoLoc.getPedidosSolicitados()) {

			double latitud = c.getDomicilio().getCoordenada().getLatitud();
			double longitud = c.getDomicilio().getCoordenada().getLongitud();

			Coordinate coord = new Coordinate(latitud, longitud);
			MapMarker marker = new MapMarkerDot(c.getCliente().getNombre(), coord);
			mapa.addMapMarker(marker);
		}

	}

}

